package com.guanyong.webapi.controllers.page;

import com.guanyong.model.*;
import com.guanyong.services.*;
import com.guanyong.webapi.config.CreateVideoURLUtil;
import com.guanyong.webapi.config.VideoLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by mxk on 2017/8/25.
 */
@Controller
public class Mobile2Home extends baseController {
    @Autowired
    CommentService commentService;

    @Autowired
    FilmServices filmServices;

    @Autowired
    VideoScoreService videoScoreService;

    @Autowired
    VideoSourceService videoSourceService;

    @Autowired
    CreateVideoURLUtil createVideoURLUtil;


    @Autowired
    SubjectService subjectService;

    @Autowired
    VideoCountService videoCountService;

    @RequestMapping({ "/", "/index2"})
    String index(HttpServletRequest request) {
        return "/home/mobile2/index";
    }

    @RequestMapping({ "/search2"})
    String search2(HttpServletRequest request) {
        return "/home/mobile2/html/search2";
    }

    @RequestMapping({ "/type"})
    String type(HttpServletRequest request) {
        return "/home/mobile2/html/types";

    }

    @RequestMapping({ "/showapi"})
    String showapi(HttpServletRequest request) {
        return "/home/mobile2/html/showapi";

    }
    @RequestMapping({ "/apidoc"})
    String apidoc(HttpServletRequest request) {
        return "/home/mobile2/html/apidoc";

    }


    @RequestMapping({ "/login2"})
    String login(HttpServletRequest request) {
        return "/home/mobile2/html/login";
    }
    @RequestMapping({ "/reg"})
    String reg(HttpServletRequest request) {
        return "/home/mobile2/html/register";
    }

    @RequestMapping({ "/forgetpassword"})
    String forgetpassword(HttpServletRequest request) {
        return "/home/mobile2/html/forgetpassword";
    }

    @RequestMapping({ "/usercenter2"})
    String usercenter2(HttpServletRequest request) {


        UserBaseModel userBaseModel=userHandle.getCurUser(request);
        if (userBaseModel!=null){
            return "/home/mobile2/html/usercenter2";
        }
        else{
            return "/home/mobile2/html/login";

        }

    }

    @RequestMapping({ "/zhuanti"})
    String zhuanti(Model model,HttpServletRequest request) {

        String subjectid=request.getParameter("subjectid");
        if (subjectid!=null)
        {
            int subjectId=Integer.parseInt(subjectid);
            SubjectModel subjectModel= subjectService.GetSubjectByIdModel(subjectid);
            if (subjectModel!=null)
            {
                model.addAttribute("subjectobj",subjectModel);
            }
        }
        return "/home/mobile2/html/zhuanti";
    }



//
//    @RequestMapping({"/mobilesearch"})
//    String mobilesearch(HttpServletRequest request) {
//
//
//        return "/home/mobile/search";
//    }
//
//    @RequestMapping({  "/movelist"})
//    String MoveList(HttpServletRequest request) {
//
//
//        return "/home/mobile/move";
//    }
//    @RequestMapping({  "/usercenter"})
//    String UserCenter(HttpServletRequest request) {
//
//
//        return "/home/mobile/usercenter";
//    }
//
    @RequestMapping({  "/video2"})
    String Video(Model model, HttpServletRequest request) {
        UserBaseModel userBaseModel=userHandle.getCurUser(request);


        String filmid=request.getParameter("fid");
        if (filmid!=null && filmid!=""){
            FilmModel filmModel=filmServices.GetFilmModelById(filmid);
            if(userBaseModel!=null){
                VideoCollectLogModel videoCollectLogModel=
                        videoCountService.GetVideoCollectByUserId(filmid,userBaseModel.getfId());
                if (videoCollectLogModel!=null ) {
                    model.addAttribute("iscollect","ok");
                }
                else{
                    model.addAttribute("iscollect","no");
                }

            }
            else{
                model.addAttribute("iscollect","no");
            }
            model.addAttribute("film",filmModel);
            VideoScoreModel videoScoreModel=videoScoreService.GetVideoScoreByObjectId(1,filmid);
            model.addAttribute("videoscore",videoScoreModel);
            FilmInfoModel filmInfoModel=filmServices.GetFilmInfoModelById(filmid);

            model.addAttribute("filminfo",filmInfoModel);
            //本地播放
            VideoSourceModel videoSourceModel=videoSourceService.
                    GetVideoScorceByObjectIdType(1,
                            filmModel.getfId(),0);
            if (videoSourceModel!=null){
                Map<String,Object> map = new HashMap<String, Object>();
                String playAuth = null;
                try {
                    if (videoSourceModel.getfSourceurl()!=null){
                        playAuth=createVideoURLUtil.getVideoURL(videoSourceModel.getfSourceLocal());
                    }
                } catch (Exception e) {
                    e.printStackTrace();

                }
                if (playAuth!=null){
                    model.addAttribute("videoVoucher",playAuth);
                }else {
                    return "errorPage/500";
                }
            }else{
                return "errorPage/500";
            }
        }
        else{
            return "errorPage/500";
        }



        return "/home/mobile2/html/video2";
    }
}
